package com.web.onirigi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.javassist.expr.NewArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.web.onirigi.model.FranchiseeBean;
import com.web.onirigi.model.OrderBean;
import com.web.onirigi.service.FranchiseeService;
import com.web.onirigi.service.OrderService;
import com.web.onirigi.util.BaseResponseObject;
import com.web.onirigi.util.BaseUtil;
import com.web.onirigi.util.CheckUtil;
import com.web.onirigi.util.ParseJsonToMap;

/**
 * 加盟商操作
 * @author ASUS
 * 创建时间  2017年10月9日 下午9:22:10
 *
 */
@RestController
@RequestMapping("franRest")
public class FranchiseeRest {

	/**
	 * 日志组件
	 */
	private static final Logger LOGGER =LoggerFactory.getLogger(FranchiseeRest.class);
	
	@Autowired
	private FranchiseeService  franchiseeServiceImpl;
	
	/**
	 * 加盟商操作
	 * @author ASUS
	 * 创建时间  2017年10月9日 下午9:23:33
	 * @param json
	 * @return
	 */
	@RequestMapping(value="fran",method= RequestMethod.POST)
	public BaseResponseObject franRest(@RequestBody String json) {
		BaseResponseObject  object = new BaseResponseObject();
		LOGGER.info("订单操作。。。。"+json);
		String[] RHtype = new String[1];
		try {
			Map map=ParseJsonToMap.parseStringToMap(json, RHtype);
			FranchiseeBean  franchiseeBean=JSON.parseObject(JSON.toJSONString(map), FranchiseeBean.class);
			Map resultMap = new HashMap<>();
			
			if( ! StringUtils.isEmpty(franchiseeBean.getFranPhone())  && !CheckUtil.checkPhoneNum(franchiseeBean.getFranPhone())) {
				object.setResponseData(resultMap);
				object.setResponseCode(BaseUtil.FAILED_CODE);
				object.setResponseMessage(BaseUtil.FAILED_PHONE);
				return object;
			}
			
			
			if(BaseUtil.REST_RHTYPE_ADD.equals(RHtype[0])) {
				franchiseeServiceImpl.addFranchiseeBean(franchiseeBean);
				
			}else if(BaseUtil.REST_RHTYPE_UPDATE.equals(RHtype[0])) {
				
				franchiseeServiceImpl.updateFranchiseeBean(franchiseeBean);
			}else if(BaseUtil.REST_RHTYPE_DELETE.equals(RHtype[0])) {
				franchiseeServiceImpl.deleteFranchiseeBean(franchiseeBean);
				
			}else {
				List<FranchiseeBean>  franList=franchiseeServiceImpl.getOFranchiseeBeans(franchiseeBean);
				resultMap.put("result", new  PageInfo<>(franList)   );
				object.setResponseData(resultMap);
			}
			
			object.setResponseCode(BaseUtil.SUCCESS_CODE);
			object.setResponseMessage(BaseUtil.ORDER_SUCCESS_CONTENT);
			return object;
			
		} catch (Exception e) {
			LOGGER.error("加盟商操作出现异常....."+json,e);
			object.setResponseCode(BaseUtil.FAILED_CODE);
			object.setResponseMessage(BaseUtil.ORDER_FAILED_CONTENT);
		}
		
		return object ;
	}
}
